import java.sql.*;

public class TestMySQLConnection {
	public static void main(String[] args) {
		ResultSet rs = null;
		Statement sta = null;
		Connection conn = null;
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/mydata?user=root&password=123456");
			sta = conn.createStatement();
			rs = sta.executeQuery("select * from dept");
			while(rs.next()) {
				System.out.println(rs.getString("deptno"));
			}
		} catch(ClassNotFoundException e) {
			e.printStackTrace();
		} catch(SQLException e) {
			System.out.println("SQLException: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("VendorError: " + e.getErrorCode());
		} finally {
			try {
				if(rs != null) {
					rs.close();
					rs = null;
				}
				if(sta != null) {
					sta.close();
					sta = null;
				}
				if(conn != null) {
					conn.close();
					conn = null;
				}
			} catch(SQLException e) {
				e.printStackTrace();
			}
			
		}
		
	}
}
